<?php

include_once "src/controller/dbmanagement/AuthorizeDBManagement.php";
include_once "src/model/User.php";
include_once "src/controller/blmanagement/UserAccountManagement.php";
include_once "src/rrsexception/EmailException.php";

/**
 * It includes all the functions for the management of the business logic
 * of the User Accounts.
 */
class AuthorizationAccountManagement {

    /**
     *  Returns User Object with user information in it if the user table contains a
     *  user with the User Id of $userId.
     */
     public static function getNewPswd() {
        $length = 8;

        //string of all possible characters to go into the new password
        $codeRandomString = "!#AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789*+";
        $len =strlen($codeRandomString);
        //initialize the new password string
        $pswd = "";

        //seed the random function
        srand((double)microtime()*1000000);

        //go through to generate a random password.
        for($x=0; $x < $length; $x++) {
            $pswd .= substr($codeRandomString,rand(0,$len-1),1);
        }
        return $pswd;
    }


    public static function login($email, $pswd) {
        try {
            $result =AuthorizeDBManagement::checkAuthorization($email,$pswd);
            if ($result==TRUE) {
                $verify= UserAccountManagement::getUserByEmail($email);
                if (!$verify) {
                    $result=FALSE;
                }
            }
            return $result;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }

     public static function forgotPassword($email) {
        try {
            $newPswd=AuthorizationAccountManagement::getNewPswd();
            $user=UserAccountManagement::getUserByEmail($email);
            if ($user != FALSE) //user exists in our system
            {
                $user->setPswd($newPswd);
                $userName=$user->getName();
                $result = UserAccountManagement::changeUserAccount($user);

                $subject='Your new password at Narf.com';
                $message= '
<html>
<head>

</head>
<body>
<center><h3>Your Narf.com Details</h3></center>
  <p>Here are your new details at Narf.com</p>
  <table>

    <tr>
      <td>Username</td><td>'.$userName.'</td>
    </tr>
    <tr>
      <td>Password</td><td>'.$newPswd.'</td>
    </tr>
  </table>
  <p>This is an autogenerated email,do not respond,please.</p>
</body>
</html>
';
            }
            else {
                if(AuthorizationAccountManagement::changeAuthorizationAccount($newPswd, $email, $email)) {
                    $subject='Your new password at Narf.com';
                    $message= '
<html>
<head>

</head>
<body>
<center><h3>Your Narf.com Details</h3></center>
  <p>You are not registered at Narf but are authorized to be added using the following reset password.</p>
  <table>

   <tr>
      <td>Password</td><td>'.$newPswd.'</td>
    </tr>
  </table>
  <p>This is an autogenerated email,do not respond,please.</p>
</body>
</html>
';
                }
                else return FALSE;
            }
            AuthorizationAccountManagement::send_email($email,$subject,$message,'teamNarf@Narf.com');
            return true;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
        catch (EmailException $e) {
            throw $e;
        }
    }


 
     public static function register($email,$pswd) {
        $result= AuthorizeDBManagement::addAuthorization($email,$pswd);
        return $result;
    }


     public static function send_email($address_to,$subject,$message,$from) {
        try {
            //// To send HTML mail, the Content-type header must be set
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            // Additional headers
            $headers .= 'To: '.$address_to.'' . "\r\n";
            $headers .= 'From: '.$from.'' . "\r\n";

            // Mail it
            if  (mail($address_to, $subject, $message, $headers)) {
                return true;
            }
            else {
                throw new EmailException("Email did not successfully send.");
            }

        }
        catch (EmailException $e) {
            throw $e;
        }
    }



    /**
     *  Returns true if database successfully completes update on
     *  user with the User Id of $userId else retruns false.
     */
    public static function changeAuthorizationAccount($newPswd, $newEmail, $oldEmail) {
        try {
            $result = AuthorizeDBManagement::updateAuthorization($newPswd, $newEmail, $oldEmail);
            return $result;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }



}
?>
